package com.ljfresh.customer.controller;

import com.ljfresh.customer.data.LoginData;
import com.ljfresh.customer.dto.udesk.CustomerCreateResult;
import com.ljfresh.customer.dto.udesk.CustomerDetailResult;
import com.ljfresh.customer.param.CustomerCreateParam;
import com.ljfresh.customer.param.CustomerParam;
import com.ljfresh.customer.pojo.Customer;
import com.ljfresh.customer.pojo.Store;
import com.ljfresh.customer.pojo.User;
import com.ljfresh.customer.result.LoginResult;
import com.ljfresh.customer.sercice.StoreService;
import com.ljfresh.customer.sercice.TwiterIdService;
import com.ljfresh.customer.utils.CheckObjectIsNullUtils;
import com.ljfresh.customer.utils.PatternUtils;
import com.ljfresh.customer.utils.RedisUtils;
import com.ljfresh.customer.utils.udesk.UDeskBasicApiUtil;
import com.ljfresh.customer.utils.udesk.UDeskConst;
import io.swagger.annotations.*;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.*;

/**
 * @author: Lucifer
 * @time: 2020/4/6-10:26
 */
@RestController
@Api(value = "用户",tags = "用户模块接口")
@RequestMapping("/user")
public class UserCoutroller {

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        System.out.print("请输入总数：");

    }


    @Autowired
    private TwiterIdService twiterIdService;

    @Resource
    private StoreService storeService;

    @Resource
    private RedisUtils redisUtils;

    @PostMapping("/login")
    @ApiOperation("登录接口")
    @ApiResponses({
            @ApiResponse(code = 200, message = "请求成功"),
            @ApiResponse(code = 400, message = "参数错误"),
            @ApiResponse(code = 105001, message = "请输入正确姓名~"),
            @ApiResponse(code = 105002, message = "请输入正确手机号~"),
            @ApiResponse(code = 105003, message = "输入密码有误，请重新输入~"),
            @ApiResponse(code = 105004, message = "参数为空"),
            @ApiResponse(code = 105005, message = "登录失败"),
            @ApiResponse(code = 105007, message = "请选择登录门店")
    })
    public CustomerDetailResult login(@RequestBody User user) {
        //参数判断
        if (CheckObjectIsNullUtils.objCheckIsNull(user)) {
            return new CustomerDetailResult(105004);
        }
        if (!user.getName().matches(PatternUtils.REGEX_SINOGRAM)) {
            return new CustomerDetailResult(105001);
        }
        if (!user.getPhone().matches(PatternUtils.REGEX_MOBILE)) {
            return new CustomerDetailResult(105002);
        }
        if (!user.getPassword().equals("lejia888")) {
            return new CustomerDetailResult(105003);
        }
        /*if (user.getStore()==null || !user.getStore().equals("")){
            return new LoginResult(105007);
        }*/
        try {
            //1.获取客户详情
            CustomerDetailResult customDetail = UDeskBasicApiUtil.getCustomDetail(UDeskConst.CUSTOMER_CELLPHONE, user.getPhone());

            if (customDetail.getCode() != 1000 && customDetail.getCode() != 2005) {
                return customDetail;//直接返回
            }
            //2.如果客户有 就不操作
            if (customDetail.getCode() == 1000 && customDetail.getCustomer().getId() != null) {
                user.setId(twiterIdService.getTwiterId());
                LoginData loginData = new LoginData();
                Store store = storeService.find("storeCode", user.getStore());
                if (!CheckObjectIsNullUtils.objCheckIsNull(store)) {
                    loginData.setStore(store);
                }
                loginData.setUser(user);
                loginData.setSessionId(twiterIdService.getSessionId());
                redisUtils.set(loginData.getSessionId(), user);
                customDetail.setLoginData(loginData);
                return customDetail;
            } else {
                //3.如果没有就创建客户
                CustomerParam customerParam = new CustomerParam(null, null, null, user.getName(), null, null, null, null, null, null, new String[][]{{null, user.getPhone()}}, null, null, null);
                CustomerCreateResult customerCreateResult = UDeskBasicApiUtil.createCustom(new CustomerCreateParam(customerParam, null, null));
                //Customer user = customerCreateResult.getCustomer();
                user.setId(twiterIdService.getTwiterId());
                LoginData loginData = new LoginData();
                Store store = storeService.find("storeCode", user.getStore());
                if (!CheckObjectIsNullUtils.objCheckIsNull(store)) {
                    loginData.setStore(store);
                }
                loginData.setUser(user);
                loginData.setSessionId(twiterIdService.getSessionId());
                redisUtils.set(loginData.getSessionId(), user);
                BeanUtils.copyProperties(customerCreateResult, customDetail);
                customDetail.setLoginData(loginData);
                //return new LoginResult(loginData);
                return customDetail;
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new CustomerDetailResult(105005);
        }
    }
}
